<?php

namespace app\admin\controller\dts\plan;

use app\common\controller\Backend;

/**
 * 圆周计划管理
 *
 * @icon fa fa-circle-o
 */
class Yuanzhou extends Backend
{

    /**
     * Yuanzhou模型对象
     * @var \app\admin\model\dts\plan\Yuanzhou
     */
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\dts\plan\Yuanzhou;

    }

    protected $weekArr = ['日','一','二','三','四','五','六'];

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    protected function before_add($params)
    {
        $contentArr = $params['content'];
        foreach($contentArr as $key => $items) {
            $time = strtotime($items['date']);
            if($time == false || strpos($items['date'], '-') == false) {
                ajaxerror('请输入正确日期，如：2020-01-01');
            }
            $contentArr[$key]['name'] = '周' . $this->weekArr[date('w', $time)];
            $contentArr[$key]['day'] = date('d', $time);
        }

        $params['content'] = json_encode(array_values($contentArr), true);

        return $params;
    }

    protected function before_edit($params, $changeData, $originData)
    {
        $arr = ['#FF9F0E', '#FF576B', '#9757FF', '#41CC58'];
        foreach($params['content'] as $key1 => $item1) {
            foreach($item1['list'] as $key2 => $item2) {
                if($item2['value']) {
                    $params['content'][$key1]['list'][$key2]['color'] = $item2['color'] ? $item2['color'] :  $arr[rand(0, count($arr) - 1)];
                }
            }
        }
        $params['content'] = json_encode($params['content']);
        return $params;
    }


    //详情
    public function details($ids = null) {

        $row = $this->model->where('id', $ids)->find();

        $this->assign('row', $row);
        return $this->view->fetch();
    }
}
